package com.he.hsdyc.config.security.token;

import com.he.hsdyc.entity.Member;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: jc
 * Date: 2019-06-14
 * Time: 15:47
 */
public class TokenUtils {

    public static Member analysisToken(HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        String token = StringUtils.substringAfter(header, "Bearer ");
        Claims body = null;
        try {
            body = Jwts.parser().setSigningKey("android".getBytes("UTF-8"))
                    .parseClaimsJws(token).getBody();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
//            throw new ValidatedException("用户信息获取失败");
        }

        Object id = body.get("id");
        String userName = (String) body.get("username");
        String tel = (String) body.get("tel");

        Member user = new Member();
        user.setId(Integer.parseInt(id.toString()));
        user.setMemberName(userName);
        user.setMobile(tel);

        return user;


    }

}
